package com.ns.school.controller.edu;

import com.ns.school.common.utils.RedisUtil;
import com.ns.school.po.edu.*;
import com.ns.school.po.sys.User;
import com.ns.school.service.edu.AddStudentService;
import com.ns.school.service.edu.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Controller
public class AddStudentController {
    @Autowired  //  自动注入
    private StudentService studentService;
    @Autowired
    private AddStudentService addStudentService;

    @Autowired
    private RedisUtil redisUtil;

    @RequestMapping("/addStudentPage")
    public String selPolitics(Model model) {
        //  查询政治面貌表
        List<PoliticsTypeDB> stupol = studentService.selPolitics();
        model.addAttribute("stupol", stupol);
        return "view/student/addStudent";
    }

    //  添加学生
    @RequestMapping("addStudent") //   用来处理请求地址映射的注解
    @ResponseBody   // 通常用来返回JSON数据给客户端
    public LayuiResult<StudentDB> addStudent(StudentVO studentVO, String birthday, String tertime) throws Exception {
        LayuiResult<StudentDB> result = new LayuiResult<>();
        //  将接收到的时间进行类型转换
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = format.parse(birthday);
        Date date2 = format.parse(tertime);
        studentVO.setSbirthday(date1);
        studentVO.setEntertime(date2);
        //  判断该年份是否已存在学生
        Integer year = addStudentService.selectStuYear(studentVO.getClassid());
        if (year != 0) {
            //  若该年份学生为空时 则添加一条分割线
            Integer fenge = addStudentService.stuSegmentation(studentVO.getClassid());
        }
        //  生成学生学号
        String stui = addStudentService.selStuid(studentVO.getGid());
        stui = new BigDecimal(stui).toPlainString();
        studentVO.setStuid(stui);
        studentVO.setSid(Integer.valueOf(stui));
        //  根据前台传来信息添加学生
        Integer addStudent = addStudentService.addStudent(studentVO);
        //  获取学号
        String stuid = studentVO.getStuid();
        //  根据学号查询学生id编号
        Integer sid = addStudentService.selectSid(stuid);
        //  获取所选专业id
        Integer mid = studentVO.getMid();
        //  根据学生专业查询该专业开设的必修课程
        List<CourseDB> selCourse = addStudentService.selCourse(mid);
        for (CourseDB cou : selCourse) {
            //  将学生id以及必修课程的id添加至学生选课表
            addStudentService.addStuCourse(sid, cou.getCid());
        }

        //  班级人数+1
        Integer selClassinfo = addStudentService.selecteClassinfo(studentVO.getClassid());
        //  年级人数+1
        Integer selGrade = addStudentService.selecteGrade(studentVO.getGid());
        //  专业人数+1
        Integer selMajor = addStudentService.selecteMajor(studentVO.getMid());
        //  系部人数+1
        Integer selDepartment = addStudentService.selecteDepartment(studentVO.getDid());

        //  根据学号查询信息
        List<StudentDB> stu = addStudentService.selectMessage(stuid);
        //  查询sys_role角色id
        String id = addStudentService.selectRole();
        result.setData(stu);
        result.setMsg(id);
        return result;
    }

    @RequestMapping("/setRedis")
    @ResponseBody
    public void setRedis() {
        User user=new User();
        user.setId(1);
        user.setPassword("123456");


        //写入缓存
        redisUtil.set("2","74");

        //写入缓存设置时效时间
        redisUtil.set("3","74",20);

        //写入缓存设置时效时间
        redisUtil.set("4","74",20, TimeUnit.SECONDS);


        user.setId(2);
        user.setPassword("1234567");

        //写入缓存，Object

        //写入缓存，Object

        redisUtil.setObject("name",user);

        //redisUtil.setObject("name",user,10);
        //redisUtil.setObject("name",user,10,TimeUnit.SECONDS);


        redisUtil.setList("key",user);


        List<Integer> list=new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        redisUtil.setList("list",list);

       // redisUtil.setList("list",list,10);
       // redisUtil.setList("list",list,10,TimeUnit.SECONDS);





        redisUtil.set("3","80");

        redisUtil.rangeByScore("2",1.0,2.0);
        System.out.println("已存入");
    }


    @RequestMapping("/getRedis")
    @ResponseBody
    public void getRedis(){

        //读取缓存
        redisUtil.get("2");
        System.out.println("2"+ redisUtil.get("2"));

        redisUtil.getObject("name",User.class);

        redisUtil.getInteger("3");
        redisUtil.getLong("3");

        redisUtil.getList("list",List.class);
    }

    //集合
    @RequestMapping("/setCc")
    @ResponseBody
    public void setRe(){
        redisUtil.add("10","10");
        redisUtil.add("11","11");
        redisUtil.add("12","12");
        redisUtil.add("13","13");
        System.out.println("集合添加成功");

        redisUtil.setMembers("12");
        System.out.println("集合:"+redisUtil.setMembers("12"));

        redisUtil.zAdd("14","4",4.0);
        redisUtil.zAdd("15","5",5.0);
        redisUtil.zAdd("16","6",6.0);
        redisUtil.zAdd("17","7",7.0);
        System.out.println("成功");

        redisUtil.rangeByScore("14",4.0,6.0);
        System.out.println("ggg"+ redisUtil.rangeByScore("14",2.0,3.0));

        //列表添加
        redisUtil.lPush("18","8");
        redisUtil.lPush("18","9");
        redisUtil.lPush("19","9");
        System.out.println("DDD");

        redisUtil.lRange("18",0,1);

        System.out.println("cc:"+redisUtil.lRange("18",0,1));

        //哈希
        redisUtil.hmSet("20","2.0","11.0");
        System.out.println("哈希：");

        redisUtil.hmGet("20","2.0");
        System.out.println("值："+redisUtil.hmGet("20","2.0"));

        //判断缓存是否有
        redisUtil.exists("20");
        System.out.println(redisUtil.exists("20"));

        //自增

        redisUtil.incr("1");
        System.out.println("aa");

    }

    //删除
    @RequestMapping("/deleteCc")
    @ResponseBody
    public void deleteRe(){
        redisUtil.remove("1");
        System.out.println("删除成功");

        redisUtil.set("2","2");
        redisUtil.set("3","3");
        redisUtil.set("4","4");

        System.out.println("存入成功");

        redisUtil.remove("2","3");
        System.out.println("删除成功2");

        redisUtil.set("user5","5");
        redisUtil.set("user6","6");
        redisUtil.set("user7","7");
        redisUtil.set("user:8","8");
        redisUtil.set("user:9","9");

        String par="*user*";
        String part="user:*";
       redisUtil.removePattern(part);

        System.out.println("删除:");




    }
}

